package com.es.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.es.dto.BrandDTO;
import com.es.entity.goods.TbBrand;
import com.es.mapper.TbBrandMapper;
import com.es.service.TbBrandService;
import com.es.utils.BeanHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 品牌表，一个品牌下有多个商品（spu），一对多关系 服务实现类
 * </p>
 *
 * @author HM
 * @since 2019-11-29
 */
@Service
public class TbBrandServiceImpl extends ServiceImpl<TbBrandMapper, TbBrand> implements TbBrandService {

    /**
     * 根据ids 查询品牌的集合
     * @param ids
     * @return
     */
    @Override
    public List<BrandDTO> findBrandListByIds(List<Long> ids) {
        Collection<TbBrand> tbBrands = this.listByIds(ids);
        List<BrandDTO> brandDTOList = tbBrands.stream()
                .map(brand -> BeanHelper.copyProperties(brand, BrandDTO.class))
                .collect(Collectors.toList());
        if(CollectionUtils.isEmpty(brandDTOList)){
            throw new IndexOutOfBoundsException();
        }
        return brandDTOList;
    }
}
